home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / ui / layout / example / GridBagWindow.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.0 KB  |  88 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18.  
  19. public class GridBagWindow extends Frame {
  20.     private boolean inAnApplet = true;
  21.  
  22.     protected void makebutton(String name,
  23.                               GridBagLayout gridbag,
  24.                               GridBagConstraints c) {
  25.         Button button = new Button(name);
  26.         gridbag.setConstraints(button, c);
  27.         add(button);
  28.     }
  29.  
  30.     public GridBagWindow() {
  31.         GridBagLayout gridbag = new GridBagLayout();
  32.         GridBagConstraints c = new GridBagConstraints();
  33.  
  34.         setFont(new Font("Helvetica", Font.PLAIN, 14));
  35.         setLayout(gridbag);
  36.    
  37.         c.fill = GridBagConstraints.BOTH;
  38.         c.weightx = 1.0;
  39.         makebutton("Button1", gridbag, c);
  40.         makebutton("Button2", gridbag, c);
  41.         makebutton("Button3", gridbag, c);
  42.  
  43.         c.gridwidth = GridBagConstraints.REMAINDER; //end of row
  44.         makebutton("Button4", gridbag, c);
  45.  
  46.         c.weightx = 0.0;                   //reset to the default
  47.         makebutton("Button5", gridbag, c); //another row
  48.  
  49.         c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
  50.         makebutton("Button6", gridbag, c);
  51.  
  52.         c.gridwidth = GridBagConstraints.REMAINDER; //end of row
  53.         makebutton("Button7", gridbag, c);
  54.  
  55.         c.gridwidth = 1;                      //reset to the default
  56.         c.gridheight = 2;
  57.         c.weighty = 1.0;
  58.         makebutton("Button8", gridbag, c);
  59.  
  60.         c.weighty = 0.0;                   //reset to the default
  61.         c.gridwidth = GridBagConstraints.REMAINDER; //end of row
  62.         c.gridheight = 1;                   //reset to the default
  63.         makebutton("Button9", gridbag, c);
  64.         makebutton("Button10", gridbag, c);
  65.     }
  66.  
  67.     public boolean handleEvent(Event e) {
  68.         if (e.id == Event.WINDOW_DESTROY) {
  69.             if (inAnApplet) {
  70.                 dispose();
  71.                 return true;
  72.             } else {
  73.                 System.exit(0);
  74.             }
  75.         }   
  76.         return super.handleEvent(e);
  77.     }
  78.  
  79.     public static void main(String args[]) {
  80.         GridBagWindow window = new GridBagWindow();
  81.         window.inAnApplet = false;
  82.  
  83.         window.setTitle("GridBagWindow Application");
  84.         window.pack();
  85.         window.show();
  86.     }
  87. }
  88.